********* TABLE A3 *********

clear all
set more off 
capture log close
 
* main 
global path "~/Dropbox/Paraguay/1_data/7_replication_dataverse"
capture cd "$path"	

*tables = analysis tables
global tables "$path/tables"


********** PANEL A: LEGITIMATE GRANTS **********
*load data
use "$path/data/indert.dta", clear

xtset id year
generate autocracy = (year<1989)

eststo clear

* Loop over dependent variables for Panel A.1 (Degree)
foreach y in grants_ha_log grants_e {
    local i = 1
    foreach fe in "year id" "year id i.depa#i.year" "year id i.district_code#i.year" "year id i.district_code#i.year i.year#i.lastname_d" {
        reghdfe `y' i.autocracy##c.zdegree [aweight=p_weights] if sample_weight == 1, absorb(`fe') cluster(id)
        
        * Store fixed effects indicators
        estadd local DT = cond("`fe'"=="year id i.depa#i.year", "Yes", "No")
        estadd local MY = cond("`fe'"=="year id i.district_code#i.year" | "`fe'"=="year id i.district_code#i.year i.year#i.lastname_d", "Yes", "No")
        estadd local FM = cond("`fe'"=="year id i.district_code#i.year i.year#i.lastname_d", "Yes", "No")
        
        summ `y' if e(sample)==1 
        estadd scalar meanDep = r(mean)
        
        local col = `i'
        if "`y'" == "grants_e" local col = `i' + 4
        eststo leg_deg`col'
        local ++i
    }
}

* Loop over dependent variables for Panel A.2 (Betweenness)
foreach y in grants_ha_log grants_e {
    local i = 1
    foreach fe in "year id" "year id i.depa#i.year" "year id i.district_code#i.year" "year id i.district_code#i.year i.year#i.lastname_d" {
        reghdfe `y' i.autocracy##c.zbetweenness [aweight=p_weights] if sample_weight == 1, absorb(`fe') cluster(id)
        
        * Store fixed effects indicators
        estadd local DT = cond("`fe'"=="year id i.depa#i.year", "Yes", "No")
        estadd local MY = cond("`fe'"=="year id i.district_code#i.year" | "`fe'"=="year id i.district_code#i.year i.year#i.lastname_d", "Yes", "No")
        estadd local FM = cond("`fe'"=="year id i.district_code#i.year i.year#i.lastname_d", "Yes", "No")
        
        summ `y' if e(sample)==1 
        estadd scalar meanDep = r(mean)
        
        local col = `i'
        if "`y'" == "grants_e" local col = `i' + 4
        eststo leg_bet`col'
        local ++i
    }
}

********** PANEL B: ILL-GOTTEN LANDS **********
*load data
use "$path/data/main.dta", clear

xtset id year
generate autocracy = (year<1989)

* Drop departments not in placebo sample
foreach dept in "caaguazu / yhu" "caazapa" "cordillera / caraguatay" "guaira" ///
    "itapua / encarnacion" "misiones / san ignacio" "neembucu / pilar" ///
    "paraguari / quiindy" "seccion occidental / chaco" {
    drop if departamento == "`dept'"
}

* Loop over dependent variables for Panel B.1 (Degree)
foreach y in tmh_ha_log tmh_e {
    local i = 1
    foreach fe in "year id" "year id i.depa#i.year" "year id i.district_code#i.year" "year id i.district_code#i.year i.year#i.lastname_d" {
        reghdfe `y' i.autocracy##c.zdegree [pweight=p_weights] if sample_weight == 1, absorb(`fe') cluster(id)
        
        estadd local DT = cond("`fe'"=="year id i.depa#i.year", "Yes", "No")
        estadd local MY = cond("`fe'"=="year id i.district_code#i.year" | "`fe'"=="year id i.district_code#i.year i.year#i.lastname_d", "Yes", "No")
        estadd local FM = cond("`fe'"=="year id i.district_code#i.year i.year#i.lastname_d", "Yes", "No")
        
        summ `y' if e(sample)==1 
        estadd scalar meanDep = r(mean)
        
        local col = `i'
        if "`y'" == "tmh_e" local col = `i' + 4
        eststo ill_deg`col'
        local ++i
    }
}

* Loop over dependent variables for Panel B.2 (Betweenness)
foreach y in tmh_ha_log tmh_e {
    local i = 1
    foreach fe in "year id" "year id i.depa#i.year" "year id i.district_code#i.year" "year id i.district_code#i.year i.year#i.lastname_d" {
        reghdfe `y' i.autocracy##c.zbetweenness [pweight=p_weights] if sample_weight == 1, absorb(`fe') cluster(id)
        
        estadd local DT = cond("`fe'"=="year id i.depa#i.year", "Yes", "No")
        estadd local MY = cond("`fe'"=="year id i.district_code#i.year" | "`fe'"=="year id i.district_code#i.year i.year#i.lastname_d", "Yes", "No")
        estadd local FM = cond("`fe'"=="year id i.district_code#i.year i.year#i.lastname_d", "Yes", "No")
        
        summ `y' if e(sample)==1 
        estadd scalar meanDep = r(mean)
        
        local col = `i'
        if "`y'" == "tmh_e" local col = `i' + 4
        eststo ill_bet`col'
        local ++i
    }
}

* Create table 
file open tableA3 using "$tables/tableA3.tex", write replace
file write tableA3 "\begin{tabular}{lcccclcccc} \hline" _n
file write tableA3 "& \multicolumn{4}{c}{Hectares (log)}  &  & \multicolumn{4}{c}{Land Grant (binary)} \\" _n 
file write tableA3 "[3pt] \cline{2-5} \cline{7-10}\\" _n
file write tableA3 "& (1) & (2) & (3) & (4) && (5) & (6) & (7) & (8) \\" _n  
file write tableA3 "\hline" _n
file write tableA3 "\multicolumn{10}{l}{\textbf{\textit{(a) Legitimate (to Peasants)}}} \\\\[7pt]" _n
file write tableA3 "\textit{(a.1) Degree Centrality} \\" _n
file close tableA3

* Panel A.1 results
esttab leg_deg* using "$tables/tableA3.tex", append fragment nonumber label style(tex) ///
    cells(b(star fmt(%12.4fc)) se(par fmt(%12.4fc))) ///
    keep(1.autocracy#c.zdegree) ///
    stats(r2, fmt(%12.4fc) label("\$R^2\$")) ///
    varlabels(1.autocracy#c.zdegree "Autocracy × Degree") ///
    extracols(5) collabels(none) mlabels(none) eqlabels(none) starlevels(* 0.1 ** 0.05 *** 0.01) ///
    noobs nonotes nogaps noline prehead("") posthead("") prefoot("") postfoot("\hline")

* Add Panel A.2 header
file open tableA3 using "$tables/tableA3.tex", write append
file write tableA3 "\textit{(a.2) Betweenness Centrality} \\" _n
file close tableA3

* Panel A.2 results with outcome mean
esttab leg_bet* using "$tables/tableA3.tex", append fragment nonumber label style(tex) ///
    cells(b(star fmt(%12.4fc)) se(par fmt(%12.4fc))) ///
    keep(1.autocracy#c.zbetweenness) ///
    stats(r2 meanDep, fmt(%12.4fc %12.4fc) label("\$R^2\$" "\hline Outcome mean")) ///
    varlabels(1.autocracy#c.zbetweenness "Autocracy × Betweenness") ///
    extracols(5) collabels(none) mlabels(none) eqlabels(none) starlevels(* 0.1 ** 0.05 *** 0.01) ///
    noobs nonotes nogaps noline prehead("") posthead("") prefoot("") postfoot("\hline")

* Add Panel B header and B.1
file open tableA3 using "$tables/tableA3.tex", write append
file write tableA3 "\multicolumn{10}{l}{\textbf{\textit{(b) Ill-Gotten (to Elites)}}} \\[7pt]" _n
file write tableA3 "\textit{(b.1) Degree Centrality} \\" _n
file close tableA3

* Panel B.1 results
esttab ill_deg* using "$tables/tableA3.tex", append fragment nonumber label style(tex) ///
    cells(b(star fmt(%12.4fc)) se(par fmt(%12.4fc))) ///
    keep(1.autocracy#c.zdegree) ///
    stats(r2, fmt(%12.4fc) label("\$R^2\$")) ///
    varlabels(1.autocracy#c.zdegree "Autocracy × Degree") ///
    extracols(5) collabels(none) mlabels(none) eqlabels(none) starlevels(* 0.1 ** 0.05 *** 0.01) ///
    noobs nonotes nogaps noline prehead("") posthead("") prefoot("") postfoot("\hline")

* Add Panel B.2 header
file open tableA3 using "$tables/tableA3.tex", write append
file write tableA3 "\textit{(b.2) Betweenness Centrality} \\" _n
file close tableA3

* Panel B.2 results with all bottom statistics
esttab ill_bet* using "$tables/tableA3.tex", append fragment nonumber label style(tex) ///
    cells(b(star fmt(%12.4fc)) se(par fmt(%12.4fc))) ///
    keep(1.autocracy#c.zbetweenness) ///
    stats(r2 meanDep N DT MY FM, fmt(%12.4fc %12.4fc %12.0fc 0 0 0) ///
    labels("\$R^2\$" "\hline Outcome mean" "\hline Observations" "Department-Year FE" "Municipality-Year FE" "Family-Year FE")) ///
    varlabels(1.autocracy#c.zbetweenness "Autocracy × Betweenness") ///
    extracols(5) collabels(none) mlabels(none) eqlabels(none) starlevels(* 0.1 ** 0.05 *** 0.01) ///
    noobs nonotes nogaps noline prehead("") posthead("") prefoot("") postfoot("\hline \end{tabular}")

